package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDTO;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jetwang
 * @version 1.0
 * @date 2022/4/12 18:37
 * SetmealController 套餐控制器
 */
@RestController
@Slf4j
@RequestMapping("setmeal")
public class SetmealController {
    private SetmealService setmealService;
    private CategoryService categoryService;
    private SetmealDishService setmealDishService;

    @Autowired
    public void setSetmealDishService(SetmealDishService setmealDishService) {
        this.setmealDishService = setmealDishService;
    }

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Autowired
    public void setSetmealService(SetmealService setmealService) {
        this.setmealService = setmealService;
    }

    /**
     * 查询套餐列表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> list(int page, int pageSize,@RequestParam(value = "name", required = false) String name) {
        // 创建分页构造器
        Page<Setmeal> setmealInfo = new Page<>(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        // 按照创建更新时间排序
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealInfo, lqw);
        Page<SetmealDTO> setmealDTOInfo = new Page<>();
        BeanUtils.copyProperties(setmealInfo, setmealDTOInfo, "records");
        List<SetmealDTO> setmealDTOList = setmealInfo.getRecords().stream().map(item -> {
            SetmealDTO setmealDTO = new SetmealDTO();
            BeanUtils.copyProperties(item, setmealDTO);
            if (categoryService.getById(item.getCategoryId()) != null) {
                Category category = categoryService.getById(item.getCategoryId());
                setmealDTO.setCategoryName(category.getName());
            }
            return setmealDTO;
        }).collect(Collectors.toList());
        setmealDTOInfo.setRecords(setmealDTOList);
        return R.success(setmealDTOInfo);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> delete(Long[] ids) {
        log.info("要删除的套餐id: {}", ids);
        setmealService.removeWithSetmealDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Cacheable(cacheNames = "setmealCache", key = "#id")
    public R<SetmealDTO> getDishById(@PathVariable Long id) {
        log.info("要查询的套餐id: {}", id);
        Setmeal setmeal = setmealService.getById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal, setmealDTO);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(lqw);
        setmealDTO.setSetmealDishes(setmealDishes);
        return R.success(setmealDTO);
    }

    /**
     * 根据ids更新套餐
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam("ids") Long[] ids) {
        log.info("要更新的套餐id为: {}", ids);
        List<Long> idList = Arrays.asList(ids);
        setmealService.updateStatusByIds(status, idList);
        return R.success("更新成功");
    }


    /**
     * 添加套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.saveWithSetmealDish(setmealDTO);
        return R.success("添加成功");
    }

    /**
     * 更新套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public R<String> update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.updateWithSetmealDish(setmealDTO);
        return R.success("更新成功");
    }
}
